package com.fruits.validator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Date;

public class TimePastConstraionValidator implements ConstraintValidator<TimePastConstraion,Object> {

    private static final Logger logger = LoggerFactory.getLogger(TimePastConstraionValidator.class);

    /**
     * 初始化
     * @param timePastConstraion
     */
    @Override
    public void initialize(TimePastConstraion timePastConstraion) {
        logger.info("过去时间戳自定义验证初始化");
    }

    /**
     * 是否有效
     * @param value
     * @param context
     * @return
     */
    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null || value == ""){
            logger.info("时间戳不能为空");
            return false;
        }
        long timestamp = Long.valueOf(value.toString());
        long nowTime = new Date().getTime();
        logger.info(timestamp +"---" + nowTime);
        if (timestamp > nowTime){
            logger.info("时间戳不能为将来的时间点");
            return false;
        }
        return true;
    }
}
